<?php
Swoole\Coroutine::set(array(
    'max_coroutine' => 4096,
));


//协程使用方法
go(function () {
    echo "开始时间：".time()."<br/>";
    $ip = '120.31.131.231'; // redis服务器ip地址
    $port = '6379'; // redis服务器端口
    $passwd = '!@#$981596276'; // redis密码
    $redis = new Swoole\Coroutine\Redis();
    $redis->connect($ip, $port);
    $redis->auth($passwd);
    $keys = "list-data";


    for ($j = 0; $j < 8; $j++) {
        $data = $redis->brPop($keys, 2);
        if (is_null($data)) {
            continue;
        }
        $data = json_decode($data[1], true);

        $db = new Swoole\Coroutine\MySQL();
        $server = array(
            'host' => '120.31.136.107',
            'user' => 'root',
            'password' => 'd23a048282d9f0e3',
            'database' => 'pay_system_v5',
            "timeout"=>5
        );
        $db->connect($server);
        if($data){
            $result = $db->query("SELECT * FROM pay_order where order_id =  '{$data['order_id']}'");
            if($result){
                $temp = request_post($result[0]['notify_url'], json_encode($result[0]));
                if($temp  == "SUCCESS" || $temp == "success"){
                    $rand = mt_rand(1000,99999);
                    $db->query("update pay_order set  platform = '{$rand}' where order_id = '{$data['order_id']}'");
                }

            }
        }
        $db->close();

    }
    echo "结束时间：".time()."<br/>";

});


function request_post($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    curl_setopt($ch, CURLOPT_TIMEOUT,35);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
}

